package com.bosssoft.boss_exam_web.Controller;

import com.bosssoft.boss_exam_core.storage.StorageService;
import com.bosssoft.boss_exam_db.entity.Filemetadata;
import com.bosssoft.boss_exam_db.service.busines.FilemetadataService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/bossExam/storage")
public class StorageController {

    // 引入存储服务
    @Autowired
    private StorageService storageService;

    // 引入文件元数据服务
    @Autowired
    private FilemetadataService filemetadataService;


    @ApiOperation(value="查询图片", notes="访问指定Key的图片" ,httpMethod="GET",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="key", value="文件对象元数据KEY", dataType = "ResponseEntity<Resource>", required=true, dataTypeClass=ResponseEntity.class),
    })
    @GetMapping("/{key:.+}")
    public ResponseEntity<Resource> fetch(@PathVariable String key) {
        Filemetadata filemetadata = filemetadataService.findByKey(key);
        if(key == null){
            ResponseEntity.notFound();
        }
        String type = filemetadata.getType();
        MediaType mediaType = MediaType.parseMediaType(type);
        Resource file = storageService.loadAsResource(key);
        System.out.println(file);
        if(file == null) {
            ResponseEntity.notFound();
        }
        return ResponseEntity.ok().contentType(mediaType).body(file);
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/29
     * @Description
     * @Param [key] 图片的key
     * @Return org.springframework.http.ResponseEntity<org.springframework.core.io.Resource>
     * @Exception
     * @since JDK 1.8
     **/
    @ApiOperation(value="文件下载", notes="根据指定Key下载图片" ,httpMethod="GET",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="key", value="文件对象元数据KEY", dataType = "ResponseEntity<Resource>", required=true, dataTypeClass=ResponseEntity.class),
    })
    @GetMapping("/download")
    public ResponseEntity<Resource> download(@RequestParam  String key) {
        Filemetadata litemallStorage = filemetadataService.findByKey(key);
        if(key == null){
            ResponseEntity.notFound();
        }
        String type = litemallStorage.getType();
        MediaType mediaType = MediaType.parseMediaType(type);
        Resource file = storageService.loadAsResource(key);
        if(file == null) {
            ResponseEntity.notFound();
        }
        return ResponseEntity.ok().contentType(mediaType).header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + file.getFilename() + "\"").body(file);
    }

}
